# -*- coding: utf-8 -*-
import oss2
import os
import sys
import json
import subprocess

projectName = sys.argv[1]
filename = sys.argv[2]  # 上传文件名

if filename == "":
    print("上传公告资源名为空！！！")
    sys.exit()

curPath = os.path.split(sys.argv[0])[0]
uploadConfigPath = os.path.join(curPath, "projectConfig.json")

ossConfig = None  # oss相关配置
pathFile = None  # 本地图片存储路径


def get_upload_config():
    with open(uploadConfigPath) as c:
        cf = json.load(c)
        global ossConfig
        global pathFile
        ossConfig = cf[projectName]["OssConfig"]
        pathFile = cf[projectName]["UploadImgSourcePath"]


get_upload_config()
ossKey = ossConfig["ossKey"]
ossPassword = ossConfig["ossPassword"]
ossBucket = ossConfig["ossBucket"]  # oss仓库名称
ossLink = None
try:
    ossLink = ossConfig["ossNode"]
except Exception as e:
    ossLink = "http://oss-cn-hangzhou.aliyuncs.com"

localFilePath = os.path.join(pathFile, filename)  # 本地文件路径

cmd1 = r"cd C:\Program Files\TortoiseSVN\bin"
cmd2 = r'svn.exe update %s --non-interactive --trust-server-cert-failures="unknown-ca,cn-mismatch,expired,not-yet-valid,other" --username Build --password zhanlong888' % pathFile
cmd = cmd1 + " && " + cmd2
subprocess.call(cmd, shell=True)

# 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
auth = oss2.Auth(ossKey, ossPassword)
# Endpoint以杭州为例，其它Region请按实际情况填写。http://resourcesdate.oss-cn-hangzhou.aliyuncs.com/ai_head/10249.jpg
bucket = oss2.Bucket(auth, ossLink, ossBucket)
result = bucket.put_object_from_file('picture/' + filename, localFilePath)
print(result.status)
